package cn.skyclass.utils
{
	import mx.collections.ArrayCollection;
	import mx.utils.ObjectUtil;
	
	/**
	 * 对 bean 的常规操作的工具类
	 */
	public class BeansUtil
	{
		/**
		 * 将 objFrom 中的所有属性值拷贝至 objTo 中
		 */
		public static function copyProperty(objTo:Object, objFrom:Object):void{
			if(objTo == null || objFrom == null) return;

			var claInfo:Object = ObjectUtil.getClassInfo(objTo);			
			var props:Array = claInfo["properties"];
			for each(var q:QName in props){
				try{		
					if(objFrom.hasOwnProperty(q.localName)){
						objTo[q.localName] = objFrom[q.localName];
					}
				}catch(err:Error){}
			}		    	
		}
		
		/**
		 * 将实例列表中的每个实例的属性提取出来，生成新的 list
		 */
		public static function pickProperty(propName:String, list:ArrayCollection):ArrayCollection{
			var out:ArrayCollection = new ArrayCollection();
			for each(var o:Object in list){
				out.addItem(o[propName]);
			}
			return out;
		}
		
		/**
		 * 将实例列表中的每个实例的属性提取出来连接成字符串
		 * eg: pickAndJoinProperty("id", new ArrayCollection([{id:1}, {id:2}, {id:3}]), ",");
		 *     --> "1,2,3";
		 */		
		public static function pickAndJoinProperty(propName:String, list:ArrayCollection, 
			separator:String=","):String{
				
			var out:String = "";
			for each(var o:Object in list){
				out += separator + o[propName].toString();
			}
			if(out){
				out = out.substring(separator.length);
			}
			return out;
		}		
	}
}